<?php
/**
 * @file functions.php
 * @package news
 * @author LD
 * @desc 函数集
 */
require_once ABSPATH.'settings.php';

/**
 * 返回tag的
 *
 * @param String $xTag
 * @return array
 */
function makeTags( $xTag ){
	$tags = preg_split('/\s|，|\.|。|-|、|,/', $xTag,  -1, PREG_SPLIT_NO_EMPTY);
	return $tags;
}//END func makeTags

/**
 * 上传文件
 *
 * 返回值:
 *  0 文件类型错误
 * 	
 * 
 */
function upFile(){
	set_time_limit(0);
	global $fileType;
	
	$a = strtolower(fileext($_FILES['file']['name']));
	//判断文件类型
	if(!in_array( strtolower(fileext($_FILES['file']['name'])), $fileType )) {
		//$text=implode(",",$fileType);
    	return 2; //echo "您只能上传以下类型文件: ",$text,"<br>";
	} else{ //生成目标文件的文件名   
   		$filename=explode(".",$_FILES['file']['name']);
   	
   	do{ 
   		$filename[0]=random(10); //设置随机数长度
     	$name=implode(".",$filename);
     	//$name1=$name.".Mcncc";
     	$uploadfile=_UPLOAD_DIR.$name;
     	// echo $uploadfile.'<br />'.$_FILES['file']['tmp_name'].'<br />';
   }while(file_exists($uploadfile));

   try {
   	if ( move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile) ){
		//$pubfile = str_replace($_SERVER['DOCUMENT_ROOT'], "http://localhost", $uploadfile);
       	return $uploadfile;
   		}
   }catch (Exception $e){
   		echo $e->getMessage();
   }
	}
	return 0;
}//END func upFile

//获取文件后缀名函数
function fileext($filename){
	return substr(strrchr($filename, '.'), 1);
}
//生成随机文件名函数   
function random($length){
	$hash = 'kkyoo-ld-';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++){
		$hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}
?>